<template>
  <div class="app-container">
    <div class="filter-container">

    </div>
    <div class="table_container">
      <el-table
        border
        v-loading="tableloading"
        :data="table_datas.slice((currentPage - 1) * limit,currentPage * limit)"
        :span-method="objectSpanMethod"
      >
        <el-table-column
          prop="day"
          label="日期"
        >
        </el-table-column>
        <el-table-column
          prop="time"
          label="检测时间"
        >
        </el-table-column>
        <el-table-column
          prop="temperature"
          label="温度"
        >
        </el-table-column>
        <el-table-column
          prop="preWaterContent"
          label="原始值"
        >
        </el-table-column>
        <el-table-column
          prop="waterContent"
          label="佑蜜值"
        >
        </el-table-column>
        <el-table-column label="是否有效">
          <template slot-scope="scope">
            <span
              style="color:green;"
              v-if="scope.row.valid ==1"
            >
              有效
            </span>
            <span
              style="color:red;"
              v-if="scope.row.valid ==2"
            >
              无效
            </span>
          </template>
        </el-table-column>
        <el-table-column label="是否优孕">
          <template slot-scope="scope">
            <span
              style="color:green;"
              v-if="scope.row.ovulation"
            >
              优孕
            </span>
          </template>
        </el-table-column>
      </el-table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          v-if="pageshow"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="table_datas.length"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { formatDateTime } from "@/utils/common";
export default {
  data() {
    return {
      count: 0,
      limit: 10,
      currentPage: 1,
      tableloading: true,
      rowList: [],
      spanArr: [],
      position: 0,
      pageshow: true
    };
  },
  props: ["table_datas", "table_loading"],
  methods: {
    initData() {
      this.tableloading = this.table_loading;
      //清空合并的数组
      this.spanArr = [];
      //刷新列表
      let list = [];
      list = this.table_datas.slice(
        (this.currentPage - 1) * this.limit,
        this.currentPage * this.limit
      );
      //合并单元格
      list.forEach((item, index) => {
        const itemTime = formatDateTime(item.detectionTime).split(" ");
        list[index].day = itemTime[0];
        list[index].time = itemTime[1];
        if (index === 0) {
          this.spanArr.push(1);
          this.position = 0;
        } else {
          if (list[index].day == list[index - 1].day) {
            this.spanArr[this.position] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.position = index;
          }
        }
      });
      setTimeout(() => {
        this.tableloading = false;
      }, 1000);
    },
    search() {
      this.currentPage = 1;
      this.pageshow = false;
      this.initData();
      this.$nextTick(() => {
        this.pageshow = true;
      });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initData();
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col
        };
      }
    }
  },
  watch: {
    table_datas: function() {
      this.initData();
    }
  }
};
</script>
